package de.gameduell.framework.game.billard.view {
	import de.gameduell.framework.game.billard.controller.IBillardController;
	import de.gameduell.framework.game.billard.event.ControllerEvent;
	import de.gameduell.framework.game.billard.event.PhysicEvent;
	
	import org.cove.ape.Vector;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;	

	/**
	 * @author cbra
	 */
	public class WhiteBallView extends BallView {
		private var multiplayerBillardController:IBillardController;
		
		public function WhiteBallView(index:int, aContent:Sprite, aController:IBillardController) {
			super(index, aContent, aController);
			multiplayerBillardController = aController;
			controller.addEventListener(ControllerEvent.BALL_IN_HAND, onBallInHand);
			physicController.addEventListener(PhysicEvent.CALCULATION_START, ballInHandOver);
			headSpotPosition = ball.position.clone();
			ballInHandArrows = content.getChildByName("ballInHandArrows") as MovieClip;	
			ballInHandArrows.visible = false;	
		}
		
		private function onBallInHand(event:ControllerEvent):void {
			if(multiplayerBillardController.breakCueBall){
				content.stage.addEventListener(MouseEvent.MOUSE_DOWN, startDragY);
				ballInHandArrows.gotoAndStop(2);
				initBallInHand();
			}
			if(multiplayerBillardController.ballInHand){
				content.stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrag);
				ballInHandArrows.gotoAndStop(1);
				initBallInHand();
			}	
		}
		
		override protected function onBallMoved(event:Event):void{
			super.onBallMoved(event);
			ballInHandArrows.x = ball.position.x;
			ballInHandArrows.y = ball.position.y;
		}
		
		private function initBallInHand():void{
			lastValidposition = ball.position.clone();
			ballInHandArrows.visible = true;	
			ballInHandArrows.x = ball.position.x;
			ballInHandArrows.y = ball.position.y;
		}
		
		private function ballInHandOver(event:Event = null):void {
			content.stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDrag);	
			content.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDrag);
			content.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDrag);
				
			content.stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDragY);	
			content.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDragY);
			content.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragY);
			ballInHandArrows.visible = false;			
		}
		
		private function startDragY(event:MouseEvent):void {	
			if(ballSprite.hitTestPoint(content.mouseX, content.mouseY) ) {
				content.stage.addEventListener(MouseEvent.MOUSE_MOVE, onDragY);
				content.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragY);
			}
		}

		private function stopDragY(event:MouseEvent):void {		
			content.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDragY);
			content.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragY);
			moveToLastValidPosition();
			physicController.placedBreakBall();
		}

		private function onDragY(event:MouseEvent):void {
			if(tableBounds.hitTestPoint(content.mouseX, content.mouseY)) {
				physicController.moveBall(ball.id, new Vector(ball.position.x, content.mouseY));
				if(physicController.whiteBallPositionValid()) {
					lastValidposition = ball.position.clone();
				}
				ballInHandArrows.y = ball.position.y;		
			}
		}
	}
}
